package cn.edu.neosoft.sunbo509.finefood.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Adapter.OrderAdapter;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.CommentBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class OrderActivity extends AppCompatActivity {
    private RecyclerView rv_order;
    private GridLayoutManager layoutmanage;
    private List<CommentBean>list;
    private OrderAdapter adapter;
    private Service service;
    private Retrofit retrofit;
    private String user_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        user_id=getIntent().getStringExtra("userid");
        InitView();
        getDate();
    }

    private void InitView() {
        rv_order = (RecyclerView) findViewById(R.id.rv_order);
        layoutmanage = new GridLayoutManager(this, 1);
        rv_order.setLayoutManager(layoutmanage);
        adapter = new OrderAdapter(getApplicationContext()) {
            @Override//因为抽象类不能实例化，因此用匿名类的方式重写OrderAdapter的showComent()
            public void showComent() {
                CommentDialogFragment mFragment = new CommentDialogFragment();
                mFragment.setCancelable(false);
                mFragment.show(getFragmentManager(), "xxx");
            }
        };
        rv_order.setAdapter(adapter);
    }
    //获取订单信息
    public void getDate(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<List<CommentBean>>call=service.getOrder(user_id);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                list=response.body();
                adapter.setList(list);
            }
            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }

}
